package cn.bocweb.spinning.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.QaMyListBean;
import cn.bocweb.spinning.util.DeviceUtil;

/**
 * Created by chenchangxing on 15/12/28.
 */
public class MyQuestionAdapter extends RecyclerView.Adapter {

    List<QaMyListBean.DataBean> mDataBeanList;

    OnClickListener onClickListener;

    public interface OnClickListener {
        void onItemClick(int position, String id);
    }

    public MyQuestionAdapter(List<QaMyListBean.DataBean> list) {
        this.mDataBeanList = list;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_question, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ViewHolder myHolder = (ViewHolder) holder;
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onItemClick(holder.getAdapterPosition(), mDataBeanList.get(holder.getAdapterPosition()).getId());
            }
        });

        myHolder.mItemQuesIcon.setVisibility(View.GONE);
        myHolder.mItemQuesLine.setVisibility(View.GONE);
        myHolder.mItemQuesText.setVisibility(View.GONE);
        myHolder.mMyQuestionAnswerCount.setVisibility(View.GONE);

        myHolder.mMyQuestionTitle.setText(mDataBeanList.get(position).getContent());

//        Date timeDate = DeviceUtil.getDateFromMillis(Long.parseLong(mDataBeanList.get(position).getTimeline()));
        myHolder.mMyQuestionTime.setText(
                DeviceUtil.getTimeByTwoTimeStampForPhp(
                        Long.parseLong(mDataBeanList.get(position).getTimeline()) * 1000,
                        System.currentTimeMillis()));

        boolean status = mDataBeanList.get(position).getIscheck().endsWith("0");
        myHolder.mMyQuestionState.setText(
                status ? "待解决" : "已解决");
        myHolder.mMyQuestionState.setTextColor(
                holder.itemView.getResources().getColor(status ? R.color.red : R.color.green));
    }

    @Override
    public int getItemCount() {
        return mDataBeanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.my_question_title)
        TextView mMyQuestionTitle;
        @Bind(R.id.my_question_state)
        TextView mMyQuestionState;
        @Bind(R.id.item_ques_icon)
        ImageView mItemQuesIcon;
        @Bind(R.id.item_ques_text)
        TextView mItemQuesText;
        @Bind(R.id.my_question_answer_count)
        TextView mMyQuestionAnswerCount;
        @Bind(R.id.item_ques_line)
        TextView mItemQuesLine;
        @Bind(R.id.my_question_time)
        TextView mMyQuestionTime;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_my_question.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */

}
